'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 生产加速
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var consumeType = require('../../../../consts/consumeType');
var consumeConf = require('../../../../config/consumeConf');
var buildType = require('../../../../consts/buildType');
var roleService = require('../../../../services/roleService');
var moment = require('moment');
var dataApi = require('../../../../libs/dataApi');
var rewardType = require('../../../../consts/rewardType.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var build_id = msg.build_id;
    var gain_type = msg.gain_type;
    var build;

    if (!build_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall([

            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'task', 'daily_task', 'achieve', 'pvp', 'build', 'tech', 'smithy', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var bag_model = player_manager.bag;
                var role_model = player_manager.role;
                var build_model = player_manager.build;
                build = player_manager.build.getBuild(build_id);

                if (!build) {
                    return next(null, {
                        code: Code.BUILD_NOT_EXIST
                    });
                }

                // gain_typ 1-金矿    2——农场
                var consume_buy_conf = dataApi.consume_buy.findById(gain_type);
                if (!consume_buy_conf) {
                    return next(null, {
                        code: Code.PARAM_ERROR
                    });
                }

                // subDiamon
                var sub_diamond = consume_buy_conf.price;
                var operation_type;

                if (gain_type === 1) {
                    operation_type = operationType.OPR_ACT_IMPROVE_PRODUCE_GOLD;
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_ACT_IMPROVE_PRODUCE, operation_type, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    build.setChestGainTime(consume_buy_conf.time, consume_buy_conf.value);
                } else {
                    operation_type = operationType.OPR_ACT_IMPROVE_PRODUCE_FOOD;
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_ACT_IMPROVE_PRODUCE, operation_type, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    build.setFarmGainTime(consume_buy_conf.time, consume_buy_conf.value);
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    build: build
                }
            });
        }
    );
};
